# Contributor: lauren n. liberda <lauren@selfisekai.rocks>
# Maintainer: lauren n. liberda <lauren@selfisekai.rocks>
pkgname=fluffychat
pkgver=1.16.0
pkgrel=0
pkgdesc="Open source, nonprofit and cute Matrix messenger app"
url="https://fluffychat.im/"
arch="aarch64 x86_64"	# flutter
license="AGPL-3.0-only"
# XXX: libolm.so not autoappended?
# zenity: pub/file_picker
depends="
	so:libolm.so.3
	zenity
	"
makedepends="
	alsa-lib-dev
	aom-dev
	brotli-dev
	crc32c-dev
	clang
	dav1d-dev
	ffmpeg-dev
	flutter-desktop
	fontconfig-dev
	freetype-dev
	glib-dev
	gn
	harfbuzz-dev
	icu-dev
	jsoncpp-dev
	libavif-dev
	libdrm-dev
	libevent-dev
	libjpeg-turbo-dev
	libpng-dev
	libsecret-dev
	libwebp-dev
	libvpx-dev
	libxml2-dev
	libxslt-dev
	lld
	llvm
	olm-dev
	openh264-dev
	opus-dev
	patchelf
	pipewire-dev
	pulseaudio-dev
	re2-dev
	snappy-dev
	woff2-dev
	zlib-dev
	"
_webrtcver=d5afc4b7be7caffbcc213c2a6486dbf3b9492763
_fwwebrtcver=1cb24a45af87a99a513f72951673fcf814a5ecb0
source="
	https://github.com/krille-chan/fluffychat/archive/${_rev:-"refs/tags/v$pkgver"}/fluffychat-${_rev:-v"$pkgver"}.tar.gz
	https://s3.sakamoto.pl/lnl-aports-snapshots/fwebrtc-$_webrtcver.tar.zst
	https://github.com/webrtc-sdk/libwebrtc/archive/$_fwwebrtcver/fwwebrtc-$_fwwebrtcver.tar.gz

	system-flutter.patch
	no-werror.patch

	wrapper.patch.webrtc
	gcc13.patch.webrtc
	use-alpine-target.patch.webrtc
	no-upgrade-debug-info.patch.webrtc
	toolhack.patch.webrtc

	fluffychat.desktop
	"
builddir="$srcdir/fluffychat-${_rev:-"$pkgver"}"
# net: pub dependencies
# !check: no tests
options="net !check"

case "$CARCH" in
	aarch64) _flutter_arch="arm64" ;;
	x86_64) _flutter_arch="x64" ;;
esac

export PUB_CACHE="$srcdir/pub_cache"

export CFLAGS="${CFLAGS/-g/} -O2 -Wno-error -Wno-unknown-warning-option -Wno-unused-command-line-argument"
export CXXFLAGS="${CXXFLAGS/-g/} -O2 -Wno-error -Wno-unknown-warning-option -Wno-unused-command-line-argument"

export CC=clang
export CXX=clang++
export AR=llvm-ar
export NM=llvm-nm
export LD=clang++

# webrtc
snapshot() {
	mkdir -p "$srcdir"
	cd "$srcdir"

	# cleanup if retrying
	rm -rf src
	rm -rf fwebrtc-$_webrtcver
	rm -f fwebrtc-$_webrtcver.tar
	rm -f fwebrtc-$_webrtcver.tar.zst

	echo "
solutions = [{
	'name': 'src',
	'url': 'https://github.com/webrtc-sdk/webrtc.git@$_webrtcver',
}]
target_cpu = ['x64', 'arm64']
target_cpu_only = True
" > .gclient

	gclient sync --no-history --nohooks --tpot-cipd-ignore-platformed

	# needed DEPS hooks
	python3 'src/build/landmines.py' --landmine-scripts 'src/tools_webrtc/get_landmines.py' --src-dir 'src'
	python3 'src/build/util/lastchange.py' -o 'src/build/util/LASTCHANGE'

	for elf in $(scanelf -RA -F "%F" src); do
		rm -f "$elf"
	done

	find src -type f -path 'test_data' -delete
	find src -type f -path 'testdata' -delete
	find src -type f -path 'ChangeLog*' -delete
	rm -rf third_party/blink/web_tests

	mv src fwebrtc-$_webrtcver

	msg "generating tarball.."
	tar -cf fwebrtc-$_webrtcver.tar \
		--exclude-backups \
		--exclude-caches-all \
		--exclude-vcs \
		fwebrtc-$_webrtcver

	zstd --auto-threads=logical --ultra --long -22 -T"${ZSTD_LIMIT:-0}" -vv fwebrtc-$_webrtcver.tar
}

prepare() {
	default_prepare

	msg "Applying more patches"
	for x in $source; do
		case "$x" in
		*.patch.webrtc)
			msg "$x"
			patch -p1 -i "$srcdir"/$x -d "$srcdir"/fwebrtc-$_webrtcver
			;;
		esac
	done

	(
		cd "$srcdir"/fwebrtc-$_webrtcver

		ln -sv "$srcdir"/libwebrtc-$_fwwebrtcver libwebrtc

		local use_system="
			brotli
			crc32c
			dav1d
			ffmpeg
			fontconfig
			freetype
			harfbuzz-ng
			icu
			jsoncpp
			libaom
			libavif
			libdrm
			libevent
			libjpeg
			libpng
			libvpx
			libwebp
			libxml
			libxslt
			openh264
			opus
			re2
			snappy
			woff2
			zlib
			"
		for _lib in $use_system libjpeg_turbo; do
			msg "Removing buildscripts for system provided $_lib"
			find . -type f -path "*third_party/$_lib/*" \
				\! -path "*third_party/$_lib/chromium/*" \
				\! -path "*third_party/$_lib/google/*" \
				\! -path './third_party/libxml/*' \
				\! -path './third_party/harfbuzz-ng/utils/hb_scoped.h' \
				\! -regex '.*\.\(gn\|gni\|isolate\|py\)' \
				-delete
		done

		msg "Replacing gn files"
		python3 build/linux/unbundle/replace_gn_files.py --system-libraries \
			$use_system

		# allow system dependencies in "official builds"
		sed -i 's/OFFICIAL_BUILD/GOOGLE_CHROME_BUILD/' \
			tools/generate_shim_headers/generate_shim_headers.py

		mkdir path
		ln -sv /usr/bin/python3 path/vpython3

		# requires py3-setuptools to always print 0...
		echo 'print(0)' > build/mac/should_use_hermetic_xcode.py
	)


	flutter pub get --enforce-lockfile

	# if there is a binary in the pub_cache, we sure did not build it.
	for elf in $(scanelf -RA -F "%F" "$PUB_CACHE"); do
		rm -f "$elf"
	done

	# use our build of the webrtc wrapper
	_fvwrtc="$(echo "$PUB_CACHE"/hosted/pub.dev/flutter_webrtc-*/third_party/libwebrtc)"
	rm -rf $_fvwrtc
	mkdir -p $_fvwrtc
	ln -sv "$srcdir"/fwebrtc-$_webrtcver/kut $_fvwrtc/lib
	ln -sv "$srcdir"/libwebrtc-$_fwwebrtcver/include $_fvwrtc/include
}

build() {
	(
		cd "$srcdir"/fwebrtc-$_webrtcver

		# shellcheck disable=2089
		local webrtc_args="
			build_with_mozilla=false
			chrome_pgo_phase=0
			clang_use_chrome_plugins=false
			clang_base_path=\"/usr\"
			custom_toolchain=\"//build/toolchain/linux/unbundle:default\"
			is_cfi=false
			is_clang=true
			is_debug=false
			is_official_build=true
			host_toolchain=\"//build/toolchain/linux/unbundle:default\"
			libwebrtc_desktop_capture=false
			proprietary_codecs=true
			rtc_build_examples=false
			rtc_build_tools=false
			rtc_enable_protobuf=false
			rtc_enable_sctp=false
			rtc_include_tests=false
			rtc_include_ilbc=false
			rtc_libvpx_build_vp9=true
			rtc_link_pipewire=true
			rtc_use_pipewire=true
			rtc_use_x11=false
			symbol_level=0
			use_custom_libcxx=false
			use_sysroot=false
		"

		msg "Building libwebrtc"
		# shellcheck disable=2090,2116
		gn gen out --args="$(echo $webrtc_args)"
		ninja -C out libwebrtc

		mkdir -p kut/linux-$_flutter_arch
		cp -v out/libwebrtc.so kut/linux-$_flutter_arch/libwebrtc.so
	)

	flutter build linux -v --release

	patchelf --set-rpath '$ORIGIN/lib' build/linux/"$_flutter_arch"/release/bundle/$pkgname
	for _elf in build/linux/"$_flutter_arch"/release/bundle/lib/lib*.so; do
		# running patchelf on libapp (the AOT snapshot of the whole Dart codebase)
		# breaks it with "[FATAL:flutter/runtime/dart_vm_initializer.cc(89)]
		# Error while initializing the Dart VM: Invalid vm isolate snapshot seen".
		#
		# it has no rpath and only links to libc soname anyway. 🤷‍♀️
		[ "$(basename "$_elf")" != "libapp.so" ] || continue

		patchelf --set-rpath '$ORIGIN' "$_elf"
	done
}

package() {
	local bundle="$builddir"/build/linux/$_flutter_arch/release/bundle

	install -Dm755 "$bundle"/$pkgname "$pkgdir"/usr/lib/$pkgname/$pkgname
	cp -rv "$bundle"/lib "$pkgdir"/usr/lib/$pkgname/lib
	cp -rv "$bundle"/data "$pkgdir"/usr/lib/$pkgname/data

	mkdir -p "$pkgdir"/usr/bin
	ln -sv /usr/lib/$pkgname/$pkgname "$pkgdir"/usr/bin/$pkgname

	install -Dm644 "$srcdir"/$pkgname.desktop "$pkgdir"/usr/share/applications/$pkgname.desktop
	install -Dm644 assets/logo.svg "$pkgdir"/usr/share/icons/hicolor/scalable/apps/im.fluffychat.Fluffychat.svg
}

sha512sums="
ac185bdadf4ca7566b181b365ee4f182c5b3725d474965638c5be6c4914cff9f10b681f674ffa0f9b20a8f088d53dd18dd9ee085b4297aaf92006a7cf3dc8e5d  fluffychat-v1.16.0.tar.gz
226a9524a58f626fcfc4226d167acae39b58ef0833121e7b8a19ae3393538c030fcdf29834061635ff02283852f92d93b4631c7cbb9d16fde7b3b46c65f3283e  fwebrtc-d5afc4b7be7caffbcc213c2a6486dbf3b9492763.tar.zst
7fc9a0ae14d396a9b6dbd969eaa060eff58e659d4c2d5ef6201bf29d64fbbf6b54d1927f8997c02803ee799eb34aa13d48a4502be291c1d17b0cbbd083998cb7  fwwebrtc-1cb24a45af87a99a513f72951673fcf814a5ecb0.tar.gz
91014708ab7bb81518ce93db0612209d6479dfa503064d9e9d3186cd5e861165a3f63e286d271d5aac8a1d676868fc3acf51648ac01c16a215062a317ccb6623  system-flutter.patch
1d2b7d28a09e26ed48d89aa5f5dffb738ba367fa2760b12980225239705e16fa1ec89901d7aecf26c8527de721a5f50f50cc77ea009cd5f25f219d1af8730ed8  no-werror.patch
158360223539c6d80b32f17d2277473292e579255609a231719d1eaddb5f258fe36421801e5ce0fedb6fe86e01e46a7a77421abc800bdfaad59ffdcfe5273aa2  wrapper.patch.webrtc
70251f951c4477e602c2e21a1a092e48e47133968fbfeb369178bc888b69c91027560ebb2c925d6b25a6b2ba6a6007523c2c42fa46fc7fbc27e727793ab52034  gcc13.patch.webrtc
d9988e1c381030a79d0fa6567e098ee94234b66af96dbf2d0a2af7a33bdd98d4dd08bbe2eeaf4f08e44f1d22319464f9b0ed5bc96cdb76c238086f9ef4af6b76  use-alpine-target.patch.webrtc
72fc71028a46f23c4d494eae22e100f048cd04071518532d5a839642d19cf8718592e7b93b4d15d40502c1a5e88d55cb3212c0edec8a3598f2d9fb2cb4f287c5  no-upgrade-debug-info.patch.webrtc
2db3cf7759bd6902e0ce4d64441318bee7436aa272501113a3b6f0ebcf810d72760f875a3bd309fd612d6d4fde9e92e7319fa053fd0f34a793cfa026ced08fc5  toolhack.patch.webrtc
c4b3c2ab656da666623a8269ae47d841bfa58153097106efbef7b7895e7845bfed0513442ac355b42b6804b4a68c597079bf4147e72c57a1906ba5b65e3150f8  fluffychat.desktop
"
